package com.junqson.je.user.controller;

import com.junqson.je.common.UserHolder;
import com.junqson.je.common.annotation.LoginRequire;
import com.junqson.je.common.model.ApiResult;
import com.junqson.je.common.model.User;
import com.junqson.je.user.model.UserModel;
import com.junqson.je.common.model.vo.SimpleUserInfo;
import com.junqson.je.user.service.UserConcernService;
import com.junqson.je.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Descriptions
 * @Company
 * @Author Junqson
 * @Date 2020/2/3 17:28
 * @Version 1.0
 */
@RestController
public class UserConcernController {


    @Autowired
    private UserService userService;

    @Autowired
    private UserConcernService userConcernService;

    @LoginRequire
    @RequestMapping("/userConcern")
    public ApiResult getConcernList() {
        User user = UserHolder.get();
        if (user != null) {
            int userId = user.getUserId();
            List<Integer> uids = userConcernService.getConcernUsers(userId);
            List<SimpleUserInfo> infos = uids.stream().map(id -> userService.getSimpleUserInfo(id))
                    .collect(Collectors.toList());
            return ApiResult.success(infos);
        }

        return ApiResult.error();
    }

    @LoginRequire
    @RequestMapping("/userFuns")
    public ApiResult getUserFuns() {
        User user = UserHolder.get();
        if (user != null) {
            int userId = user.getUserId();
            List<Integer> uids = userConcernService.getConcernBy(userId);
            List<SimpleUserInfo> infos = uids.stream().map(id -> userService.getSimpleUserInfo(id))
                    .collect(Collectors.toList());
            return ApiResult.success(infos);
        }
        return ApiResult.error();
    }


    @LoginRequire
    @RequestMapping("/concern")
    public ApiResult concernUser(Integer concernUserId) {
        if (concernUserId == null) {
            return ApiResult.error("concern user id is null");
        }
        User thisUser = UserHolder.get();
        if (thisUser != null) {
            UserModel userModel = userService.getUserById(concernUserId);
            if (userModel == null) {
                return ApiResult.error("关注的用户不存在");
            }
            int userId = thisUser.getUserId();
            return userConcernService.concernOrCancel(userId, concernUserId);
        }
        return ApiResult.error();
    }
}
